package seotools.positions;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import util.Helper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class WebasystPrice {
    public static final String DELIMETER = ";";
    public static final String[] keywords = new String[]{
            "купить", "продажа", "цена", "стоимость", "заказать", "доставка", "почтой", "магазин", "Санкт Петербург", "аккумулятор", "зарядное устройство", "корпус", "пленка", "картридер"
    };

    public static void main(String[] args) throws IOException {
        //         FileInputStream is = new FileInputStream("price.xml");
//        Document doc = Jsoup.parse(new File("price.xml"), "UTF-8");
        Document doc = Jsoup.parse(new File("hotprice.xml"), "UTF-8");
//        System.out.println(doc);
//        Elements rows = doc.select("cell[ss:styleid=s36] data");
//        PrintStream out = new PrintStream(new FileOutputStream("webasyst-price.txt"));
        PrintStream out = new PrintStream(new FileOutputStream("webasyst-hot-price.txt"));
        out.println("Артикул" + DELIMETER +
                "Наименование (Русский)" + DELIMETER +
                "ID страницы (часть URL используется в ссылках на эту страницу)" + DELIMETER +
                "Цена" + DELIMETER +
                "На складе" + DELIMETER +
                "Краткое описание (Русский)" + DELIMETER +
                "Описание (Русский)" + DELIMETER +
                "Заголовок страницы (Русский)" + DELIMETER +
                "Тег META keywords (Русский)" + DELIMETER +
                "Тег META description (Русский)");
        Element table = doc.select("table").get(0);
        Elements rows = table.children();
        Category currCat = null;
        List<Item> items = new ArrayList<Item>();
        for (int i = 0; i < rows.size(); i++) {
            if (rows.get(i).children().size() == 0) {
                continue;
            }
            String attr = rows.get(i).child(0).attr("ss:StyleID");
            String name = rows.get(i).child(0).text();
            if (attr.equals("s68")) { // parent category
                Category cat = new Category();
                cat.name = name;
                cat.level = 0;
                currCat = cat;
                out.println(DELIMETER + cat.name + DELIMETER + Helper.translitUrl(cat.name) + DELIMETER + DELIMETER + DELIMETER + DELIMETER + name + metaKeywords() + DELIMETER + name + metaKeywords() + DELIMETER + name + metaKeywords());
            } else if (attr.equals("s71")) {
                while (currCat.level != 0) {
                    currCat = currCat.parent;
                }
                Category cat = new Category();
                cat.name = name;
                cat.level = 1;
                cat.parent = currCat;
                currCat = cat;
                out.println(DELIMETER + "!" + cat.name + DELIMETER + Helper.translitUrl(cat.name) + DELIMETER + DELIMETER + DELIMETER + DELIMETER + name + metaKeywords() + DELIMETER + name + metaKeywords() + DELIMETER + name + metaKeywords());
            } else if (attr.equals("s78")) {
                while (currCat.level != 1) {
                    currCat = currCat.parent;
                }
                Category cat = new Category();
                cat.name = name;
                cat.level = 2;
                cat.parent = currCat;
                currCat = cat;
                out.println(DELIMETER + "!!" + cat.name + DELIMETER + Helper.translitUrl(cat.name) + DELIMETER + DELIMETER + DELIMETER + DELIMETER + name + metaKeywords() + DELIMETER + name + metaKeywords() + DELIMETER + name + metaKeywords());
//            } else if (attr.equals("s36")) {
            } else if (attr.equals("s74")) {
                Item item = new Item();
                item.name = name;
                item.id = Helper.translitUrl(name);
                item.desc = "Описание товара: " + name;
                item.smallDesc = "Краткое описание товара: " + name;
                item.header = name + metaKeywords();
                item.metaKeywords = name + metaKeywords();
                item.metaDescription = name + metaKeywords();
                item.count = rows.get(i).child(1).text();
                item.code = rows.get(i).child(2).text();
                item.priceHigh = rows.get(i).child(3).text();
                item.priceMedium = rows.get(i).child(5).text();
                item.priceSmall = rows.get(i).child(7).text();
                item.publish = "Y";
                item.category = currCat;
                items.add(item);
                out.println(item.code + DELIMETER +
                        item.name + DELIMETER +
                        item.id + DELIMETER +
                        item.priceSmall + DELIMETER +
                        item.count + DELIMETER +
                        item.smallDesc + DELIMETER +
                        item.desc + DELIMETER +
                        item.header + DELIMETER +
                        item.metaKeywords + DELIMETER +
                        item.metaDescription
                );
            }
        }
//        System.out.println(items.size());
//        for (Item item : items) {
//            System.out.println(item);
//            out.println(item);
//        }
    }

    private static String metaKeywords() {
        StringBuilder sb = new StringBuilder(" ");
        for (String keyword : keywords) {
            sb.append(keyword).append(" ");
        }
        return sb.toString();
    }

    static class Category {
        String name;
        int level;
        Category parent;

        @Override
        public String toString() {
            if (parent != null) {
                return parent + "/" + name;
            } else {
                return name;
            }
        }
    }

    static class Item {
        String name;
        String desc;
        String id;
        String count;
        String code;
        String priceSmall;
        String priceMedium;
        String priceHigh;
        String publish;
        Category category;
        String smallDesc;
        String header;
        String metaKeywords;
        public String metaDescription;

        @Override
        public String toString() {
            return category + DELIMETER + name + DELIMETER + id + DELIMETER + desc + DELIMETER + count + DELIMETER + code + DELIMETER + priceSmall + DELIMETER + publish;
        }
    }
}
